package com.ruoyi.system.controller;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.Pro;
import com.ruoyi.system.service.IProService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2024-07-24
 */
@RestController
@RequestMapping("/pro")
public class ProController extends BaseController
{
    @Autowired
    private IProService proService;

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("system:pro:list")
    @GetMapping("/list")
    public TableDataInfo list(Pro pro)
    {
        startPage();
        Integer num =  0;
        List<Pro> list = proService.selectProList(pro);
        for (Pro pro1 : list) {
            if(pro1.getPid()==0){
                num++;
            }
        }
        if(num==0){
            return getDataTable(list);
        }
        for (Pro perm : list) {
            //3.从所有的菜单集合中获取菜单的子菜单
            List<Pro> children = list.stream().filter(p -> p.getPid() == perm.getId()).collect(Collectors.toList());
            //4.把查询到的子菜单，设置到父菜单中
            perm.setChildren(children);
        }

        TableDataInfo dataTable = getDataTable(list.stream().filter(p -> p.getPid() == 0).collect(Collectors.toList()));
        return dataTable;
    }



    @RequiresPermissions("system:pro:groupById")
    @GetMapping("/groupById")
    public List<Map<String, Object>> getGroupBy(Pro pro)
    {
//        impl处理错了，思路后续更改为获取统一的map来存储所有数据。因为前台需要的是一个list包含map
        ArrayList<Map<String, Object>> list = new ArrayList<>();
//        获取所有的查询到的map例如 商品=88
        HashMap<String, Integer> maps = proService.selectGroup(pro);
//        遍历属性
        for (Map.Entry<String, Integer> entry : maps.entrySet()) {
//            获取名称和数据分别存储到新的map中做数据存储
            String key = entry.getKey();
            Integer value = entry.getValue();
            HashMap<String, Object> map = new HashMap<>();
            map.put("date", key);
            map.put("count", value);
//            把map添加到要返回的list中
            list.add(map);
        }
        return list;
    }


    @GetMapping("/statusChange")
    public AjaxResult statusChange(String status,String id)
    {
        System.err.println(id+"status"+status+"dasdasddsasdas");
        int i = proService.update(status,id);
        return toAjax(i);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("system:pro:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Pro pro)
    {
        List<Pro> list = proService.selectProList(pro);
        ExcelUtil<Pro> util = new ExcelUtil<Pro>(Pro.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @RequiresPermissions("system:pro:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(proService.selectProById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @RequiresPermissions("system:pro:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody Pro pro)
    {
        return toAjax(proService.insertPro(pro));
    }


    /**
     * 修改【请填写功能名称】
     */
    @RequiresPermissions("system:pro:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Pro pro)
    {
        return toAjax(proService.updatePro(pro));
    }



    @GetMapping("/getType")
    public AjaxResult getType()
    {
        List<Pro> list = proService.selectProList(new Pro());
        List<Pro> list1 = new ArrayList<>();
        for (Pro pro : list) {
            if (pro.getPid() == 0){
                Pro pro1 = new Pro();
                pro1.setId(pro.getId());
                pro1.setName(pro.getName());
                list1.add(pro1);
            }
        }
        System.out.println(list1+"====");
        return success(list1);
    }


    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("system:pro:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(proService.deleteProByIds(ids));
    }
}
